package com.sms.graduate.controller;

import com.sms.graduate.entity.SmsHostel;
import com.sms.graduate.entity.SmsUser;
import com.sms.graduate.entity.dto.HostelDTO;
import com.sms.graduate.service.SmsHostelService;
import com.sms.graduate.service.SmsUserService;
import com.sms.graduate.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/hostel")
@Api(tags = "宿舍信息")
@CrossOrigin(origins = "*")
public class HostelController {

    @Autowired
    SmsHostelService smsHostelService;

    @Autowired
    SmsUserService smsUserService;

    @GetMapping("/queryHostelInfo")
    @ApiOperation("查询宿舍信息")
    public R queryHostelInfo(SmsHostel smsHostel){
        try {
            List<SmsHostel> hostelList = smsHostelService.queryHostelInfo(smsHostel);
            return R.ok().put("data",hostelList);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @PostMapping("/addHostel")
    @ApiOperation("新增宿舍")
    public R addHostel(SmsHostel smsHostel){
        try {
            if (smsHostelService.addHostel(smsHostel)) {
                return R.ok("新增成功");
            }else {
                return R.ok("宿舍号重复或输入值有误");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @PostMapping("/addPeopleToHostel")
    @ApiOperation("宿舍增加人员")
    public R addPeopleToHostel(@RequestBody HostelDTO hostelDTO){
        try {
            Integer id = hostelDTO.getId();
            List<String> userList = hostelDTO.getUserList();
            SmsHostel byId = smsHostelService.getById(id);
            if (byId.getPeopleNum()>=userList.size()){
                userList.forEach(a->{
                  smsUserService.updateUserHostel(byId.getHostelCode(),a);
                  smsHostelService.loseHostelNum(id);
                });
            }else {
                return R.ok("人员数量大于宿舍空床数量");
            }
            return R.ok("人员安排成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    @PostMapping("/deletePeopleToHostel")
    @ApiOperation("从宿舍删除人员")
    public R deletePeopleToHostel(@RequestBody HostelDTO hostelDTO){
        try {
            List<String> userList = hostelDTO.getUserList();
            //SmsHostel byId = smsHostelService.getById(hostelDTO.getId());
            userList.forEach(a->{
                smsUserService.updateUserHostel(null,a);
                smsHostelService.addHostelNum(hostelDTO.getId());
            });
            return R.ok("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/queryHostelPeople")
    @ApiOperation("查询宿舍人员")
    public R queryHostelPeople(@RequestParam("id") Integer id){
        try {
            SmsHostel byId = smsHostelService.getById(id);
            List<SmsUser> userList = smsUserService.queryHostelPeople(byId.getHostelCode());
            return R.ok().put("data",userList);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/deleteHostel")
    @ApiOperation("删除宿舍")
    public R deleteHostel(@RequestParam("id") Integer id){
        try {
            if (smsHostelService.deleteHostel(id)){
                return R.ok("删除成功");
            }else {
                return R.ok("删除失败，请重试");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


}
